<template>
  <view class="container">
    <!-- #ifdef MP-WEIXIN -->
    <view class="nav-bar">
      <view class="content">
        <view class="nav-bar-left">
          <view class="title">万选回收</view>
          <view>WAN XUAN HUI SHOU</view>
        </view>
        <view class="nav-bar-right">
          <text>全国</text>
          <up-icon name="arrow-right" color="#fff" size="14"></up-icon>
        </view>
      </view>
    </view>
    <!-- #endif -->
    <!-- 主内容区域 -->
    <view class="main-content">
      <!-- Banner轮播 -->
      <up-swiper :list="banner" indicator indicatorMode="line" :autoplay="true" :interval="3000" radius="20rpx"
        :duration="500" :show-dots="true" :show-desc="true" height="240rpx" keyName="image"></up-swiper>


      <view class="box">
        <!-- 功能导航 -->
        <view class="nav-grid">
          <view class="nav-item" v-for="(item, index) in navigations" :key="index" @tap="navigateTo(item.type)">
            <image :src="item.image" class="nav-icon"></image>
            <text class="nav-text">{{ item.name }}</text>
          </view>
        </view>

        <!-- 一键回收按钮 -->
        <button open-type="contact" class="recycle-btn" @tap="addorder">
          一键上门回收
        </button>

        <!-- 价格入口 -->
        <view class="price-entry">
          <view class="price-item" @tap="skip('/pages/subPages/blacklist')">
            <view class="price-info">
              <text class="price-title">黑名单查询</text>
              <text class="price-desc">实时更新</text>
            </view>
          </view>
          <view class="price-item" @click="toMenu">
            <view class="price-info">
              <text class="price-title">回收行情价</text>
              <text class="price-desc">实时更新</text>
            </view>
          </view>
        </view>
      </view>

      <!-- 邮寄信息 -->
      <view class="mail-section">
        <view class="notice-bar" @tap="toNotice">
          <up-icon name="volume-fill" size="18" color="#e3512b"></up-icon>
          <view class="up-line-1 notice-text">{{ notice }}</view>
          <up-icon name="arrow-right" size="14" color="#999"></up-icon>
        </view>
        <view class="mail-content">
          <view class="mail-title title">自行邮寄
          </view>
          <view class="store-info">
            <view class="store-header">
              <text class="store-name">{{ address.mailing_address_name }}</text>
              <text class="store-phone">{{ address.mailing_address_phone }}</text>
            </view>
            <text class="store-address">{{ address.mailing_address_info }}</text>
            <view @tap="copyaddress(address)" class="copy-btn">复制回收地址</view>
          </view>

        </view>
      </view>

      <!-- 回收流程 -->
      <view class="box" style="border-bottom-right-radius: 0;border-bottom-left-radius: 0;">
        <view class="section-title title">回收流程 </view>
        <view class="process-steps">
          <view class="step-item" v-for="(step, index) in processSteps" :key="index">
            <image :src="step.icon" class="step-icon"></image>
            <text class="step-text">{{ step.text }}</text>
          </view>
        </view>
      </view>

      <!-- 常见问题 -->
      <view class="section">
        <up-collapse :border="false">
          <up-collapse-item v-for="(item, index) in articles" :key="item.id" :title="item.question"
            :name="item.question">
            <text class="collapse-content">{{ item.answer }}</text>
          </up-collapse-item>
        </up-collapse>
      </view>

      <!-- 微信群 -->
      <view class="box">
        <view class="section-title title">添加微信群
        </view>
        <view class="qrcode-box">
          <view class="qrcode-wrapper">
            <image @tap="showQrcode" mode="aspectFit" :show-menu-by-longpress="true" :src="qrcode" class="qrcode"
              v-if="qrcode"></image>
            <up-empty v-else mode="data" iconSize="100"></up-empty>
          </view>
          <text class="qrcode-tip">添加微信群，了解更多回收信息</text>
        </view>
      </view>
    </view>
    <customerServiceFloat />

    <up-overlay :show="showNotice" mask-click-able>
      <view class="popup">
        <view class="daily_reminder">
          <image :src="daily_reminder" mode="widthFix"></image>
        </view>
        <view class="close-icon" @click="closeNotice">
          <up-icon name="close" size="22" color="#333"></up-icon>
        </view>
      </view>

    </up-overlay>


  </view>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { onShareAppMessage, onShareTimeline } from '@dcloudio/uni-app'
import customerServiceFloat from '@/components/customerServiceFloat/index.vue'
import { commonApi } from '@/api/common';
import { questionApi } from '@/api/question'
import { blockApi } from '@/api/block'

onShareAppMessage(() => {
  return {
    title: '万选回收',
    path: '/pages/tabbar/index'
  }
})

onShareTimeline(() => {
  return {
    title: '万选回收',
  }
})





const showNotice = ref(false)
const daily_reminder = ref('')
const notice = ref(['平台所有产品都支持顺丰上门/实时回款'])
const banner = ref([
  { img: 'https://img0.baidu.com/it/u=2525268224,3760863564&fm=253&fmt=auto&app=120&f=JPEG?w=889&h=500' },
  { img: 'https://img0.baidu.com/it/u=2525268224,3760863564&fm=253&fmt=auto&app=120&f=JPEG?w=889&h=500' },
])
const articles = ref([])
const qrcode = ref('')

// 关闭公告
const closeNotice = () => {
  showNotice.value = false
  const today = new Date();
  const todayStr = `${today.getFullYear()}-${today.getMonth() + 1}-${today.getDate()}`;
  uni.setStorageSync('notice_last_shown_date', todayStr);
}


const navigations = [
  { image: '/static/classify/phone.png', name: '手机', type: 'phone' },
  { image: '/static/classify/game.png', name: '电玩', type: 'game' },
  { image: '/static/classify/computer.png', name: '电脑', type: 'computer' },
  { image: '/static/classify/accessories.png', name: '数码配件', type: 'digital' },
  { image: '/static/classify/gold.png', name: '黄金', type: 'gold' },
]
const processSteps = [
  { icon: '/static/step/1.png', text: '一键下单' },
  { icon: '/static/step/2.png', text: '顺丰上门' },
  { icon: '/static/step/3.png', text: '实时回款' },
  { icon: '/static/step/4.png', text: '专业回收' },
]

// 复制回收地址
const copyaddress = (address) => {
  uni.setClipboardData({
    data: `${address.mailing_address_name} ${address.mailing_address_phone} ${address.mailing_address_info}`,
    success() {
      uni.showToast({ icon: 'none', title: '复制成功' })
    },
  })
}


// 预览微信群二维码
const showQrcode = () => {
  uni.previewImage({ urls: [qrcode.value], current: qrcode.value })
}

// 跳转页面
const skip = (url) => {
  if (url) {
    uni.navigateTo({
      url
    })
  }
}

// 自行邮寄地址
const address = ref({})

// 获取自行邮寄地址
const getAddress = async () => {
  try {
    const res = await commonApi.address();
    address.value = res.data;
    console.log(address.value, "address");
  } catch (error) {
    console.log(error, "error");
  }
}

// 获取微信群二维码
const getQrCode = async () => {
  try {
    const res = await commonApi.QrCode();
    qrcode.value = res.data?.QR_CODE;
    daily_reminder.value = res.data?.daily_reminder;
    // 判断是否显示公告
    if (daily_reminder.value) {
      const today = new Date();
      const todayStr = `${today.getFullYear()}-${today.getMonth() + 1}-${today.getDate()}`;
      const lastShownDate = uni.getStorageSync('notice_last_shown_date');
      if (lastShownDate != todayStr) {
        showNotice.value = true;
      }
    }
  } catch (error) {
    console.log(error, "error");
  }
}

//获取常用问题
const getQuestion = async () => {
  try {
    const res = await questionApi.getQuestionList();
    console.log(res, "res");
    articles.value = res.data;
  } catch (error) {
    console.log(error, "error");
  }
}

const toMenu = () => {
  uni.switchTab({ url: '/pages/tabbar/menu' })
}

const getBlockList = async () => {
  try {
    const res = await blockApi.getBlockList();
    banner.value = res.data;
  } catch (error) {
    console.log(error, "error");
  }
}

//获取公告标题  
const getNoticeTitle = async () => {
  try {
    const res = await commonApi.config({
      type: 'announcement_title'
    });
    notice.value = res.data?.value;
  } catch (error) {
    console.log(error, "error");
  }
}

const toNotice = () => {
  uni.navigateTo({
    url: '/pages/subPages/notice'
  })
}
onMounted(() => {
  getNoticeTitle()
  getBlockList()
  getAddress();
  getQrCode();
  getQuestion()
})
</script>

<style lang="scss" scoped>
@import '@/index.scss';

.container {
  min-height: 100vh;
  background: #F7F7F7;
  position: relative;

  .nav-bar {
    background: $primary;
    color: #fff;
    position: sticky;
    top: 0;
    left: 0;
    width: 100%;
    padding-bottom: 40rpx;

    .content {
      padding-top: 44px;
      height: 44px;
      display: flex;
      align-items: center;
      gap: 20rpx;

      .nav-bar-left {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        font-size: 15rpx;
        color: #fff;
        padding-left: 26rpx;

        .title {
          font-size: 44rpx;
          font-weight: 700;
        }
      }

      .nav-bar-right {
        font-size: 32rpx;
        color: #fff;
        opacity: 0.8;
        display: flex;
        align-items: center;
        gap: 4rpx;
      }
    }
  }

  .main-content {
    width: 100%;
    height: calc(100vh - 92px);
    position: absolute;
    top: 92px;
    left: 0;
    right: 0;
    bottom: 0;
    border-top-left-radius: 30rpx;
    border-top-right-radius: 30rpx;
    padding: 24rpx;
    box-sizing: border-box;
    background: linear-gradient(180deg, #fff 0%, #f7f7f7 100%);
  }

  .box {
    background-color: #fff;
    border-radius: 16rpx;
    padding: 32rpx 28rpx;
    box-sizing: border-box;
    margin-top: 24rpx;
    display: flex;
    flex-direction: column;
  }


  .nav-grid {
    display: flex;
    flex-wrap: wrap;
    gap: 20rpx;

    .nav-item {
      flex: 1;
      display: flex;
      flex-direction: column;
      align-items: center;
      margin-bottom: 20rpx;

      .nav-icon {
        width: 96rpx;
        height: 96rpx;
      }

      .nav-text {
        font-size: 24rpx;
        color: #222;
        margin-top: 6rpx;
      }
    }
  }

  .recycle-btn {
    width: 100%;
    height: 88rpx;
    margin-top: 32rpx;
    background: $primary;
    border-radius: 38px;
    color: #fff;
    font-size: 28rpx;
    display: flex;
    align-items: center;
    justify-content: center;
  }

  .price-entry {
    width: 100%;
    display: flex;
    justify-content: space-between;
    margin-top: 32rpx;
    gap: 22rpx;

    .price-item {
      flex: 1;
      height: 152rpx;
      border-radius: 16rpx;
      display: flex;
      align-items: center;
      padding: 0 20rpx;
      background-size: 100% 100%;
      background-repeat: no-repeat;
      background-position: center;


      &:nth-child(2) {
        background-image: url('/static/box1.png');
      }

      &:nth-child(1) {
        background-image: url('/static/box2.png'), linear-gradient(135deg, #F2FDFC 0%, rgba(225, 250, 247, 0.5) 100%);
      }

      .price-info {
        display: flex;
        flex-direction: column;

        .price-title {
          font-size: 28rpx;
          color: #222;
          font-weight: 500;
        }

        .price-desc {
          font-size: 24rpx;
          color: #666;
          margin-top: 25rpx;
        }
      }

      .price-icon {
        width: 88rpx;
        height: 88rpx;
      }
    }
  }

  .mail-section {
    background: #FFE6E0;
    border-radius: 16rpx;
    margin-top: 20rpx;
    overflow: hidden;

    .notice-bar {
      display: flex;
      align-items: center;
      justify-content: space-between;
      gap: 10rpx;
      font-size: 24rpx;
      color: #666;
      padding: 14rpx 24rpx;

      .notice-text {
        flex: 1;
      }


    }


    .mail-content {
      background: #fff;
      border-radius: 8px;
      padding: 18rpx 24rpx;
      border-start-start-radius: 16rpx;
      border-start-end-radius: 16rpx;

      .mail-title {
        font-size: 32rpx;
        font-weight: 500;
        color: #222;
        margin-bottom: 20rpx;
      }


      .store-logo {
        width: 118rpx;
        height: 90rpx;
      }

      .store-info {
        flex: 1;
        margin-left: 20rpx;

        .store-header {
          display: flex;
          align-items: center;

          .store-name {
            font-size: 28rpx;
            color: #222;
            font-weight: 500;
          }

          .store-phone {
            font-size: 28rpx;
            color: #222;
            margin-left: 16rpx;
          }
        }

        .store-address {
          font-size: 24rpx;
          color: #666;
          margin-top: 8rpx;
        }

        .copy-btn {
          width: fit-content;
          background: $light;
          border-radius: 50rpx;
          padding: 10rpx 24rpx;
          font-size: 24rpx;
          color: #333333;
          margin-top: 20rpx;
          font-weight: 600;
        }
      }

    }
  }

  .section {
    border-radius: 0 0 16rpx 16rpx;
    overflow: hidden;
  }

  .collapse-content {
    font-size: 24rpx;
    color: #999999;
  }

  .process-steps {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 20rpx 24rpx;
    background: #F7F7F7;
    margin-top: 20rpx;
    border-radius: 16rpx;

    .step-item {
      display: flex;
      flex-direction: column;
      align-items: center;

      .step-icon {
        width: 80rpx;
        height: 80rpx;
      }

      .step-text {
        font-size: 24rpx;
        color: #666;
        margin-top: 10rpx;
      }

      .step-arrow {
        width: 46rpx;
        height: 16rpx;
        margin: 0 10rpx;
      }
    }
  }

  .faq-content {
    padding: 20rpx;
    color: #888;
  }

  .qrcode-box {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 20rpx;

    .qrcode-wrapper {
      width: 360rpx;
      height: 360rpx;
      border-radius: 20rpx;
      margin-top: 32rpx;
      background: #F6F6F6;
      display: flex;
      align-items: center;
      justify-content: center;

      .qrcode {
        width: 280rpx;
        height: 280rpx;
      }
    }

    .qrcode-tip {
      margin-top: 28rpx;
      font-size: 24rpx;
      color: #666;
    }
  }

  .float-btn {
    position: fixed;
    right: 30rpx;
    bottom: 120rpx;
    z-index: 999;
    background: none;
    padding: 0;
    margin: 0;
    line-height: 1;

    &::after {
      border: none;
    }

    .float-btn-content {
      .float-icon {
        width: 116rpx;
        height: 138rpx;
      }
    }
  }

  .update-popup {
    width: 560rpx;
    background: #fff;
    border-radius: 24rpx;
    overflow: hidden;

    .update-img {
      width: 100%;
      height: 248rpx;
    }

    .update-content {
      padding: 88rpx 0;
      display: flex;
      flex-direction: column;
      align-items: center;

      .update-title {
        font-size: 36rpx;
        font-weight: 700;
        color: #222;
      }

      .update-desc {
        font-size: 28rpx;
        color: #666;
        margin-top: 8rpx;
      }

      .update-btn {
        width: 464rpx;
        height: 88rpx;
        line-height: 88rpx;
        text-align: center;
        background: $primary;
        border-radius: 450rpx;
        color: #222;
        font-size: 32rpx;
        font-weight: 700;
        margin-top: 48rpx;
      }
    }
  }

  .title {
    display: flex;
    align-items: center;

    &::before {
      content: '';
      display: inline-block;
      width: 12rpx;
      height: 32rpx;
      background: $primary;
      border-radius: 6rpx;
      margin-right: 12rpx;
    }
  }
}

:deep(.u-collapse-item__content__text) {
  background-color: #F7F7F7;
}

:deep(.u-collapse-item) {
  background-color: #fff;
}

:deep(.u-cell__title) {
  color: #999 !important;
  font-size: 28rpx !important;
}

:deep(.u-collapse-item .u-icon__icon) {
  color: #999 !important;
  font-size: 28rpx !important;
}

.popup {
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  padding: 30rpx;
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 80vw;
  border-radius: 20rpx;

  .close-icon {
    position: absolute;
    bottom: -10%;
    right: 50%;
    transform: translateX(50%);
    background: #fff;
    border-radius: 50%;
    padding: 10rpx;
    z-index: 9999;
  }

  .daily_reminder {
    width: 100%;
    height: 100%;

    image {
      width: 100%;
      height: 100%;
    }
  }


}
</style>